#include "gui.h"
#include "data_ops.h"
#include <gtk/gtk.h>
#include <time.h>
#include <string.h>
#include<glib/gdate.h>
// 全局变量
extern char current_admin[20];

static void process_card_scan(const char *card_id);
static void on_admin_login_clicked(GtkWidget *widget, gpointer data);
static void login_clicked(GtkWidget *widget, gpointer data);
static void register_save_clicked(GtkWidget *widget, gpointer data);
static void query_by_card_clicked(GtkWidget *widget, gpointer data);
static void on_card_input_changed(GtkWidget *entry, gpointer user_data);
static void query_by_name_clicked(GtkWidget *widget, gpointer data);
static void on_refresh_clicked(GtkWidget *w, gpointer d);
static void manage_deactivate_clicked(GtkWidget *widget, gpointer data);
static void save_member_changes(GtkWidget *widget, gpointer data);
static void modify_member_clicked(GtkWidget *widget, gpointer data);
static void record_query_clicked(GtkWidget *widget, gpointer data);

// 实现read_card_id函数
// 这个函数模拟读取卡号，可以根据实际读卡器进行修改
int read_card_id(char *card_id, int max_len) {
    return 0; // 成功读取
}

// 处理卡片扫描
static void process_card_scan(const char *card_id) {
    if (strlen(card_id) != 10) {
        GtkWidget *dialog = gtk_message_dialog_new(
            NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "卡号格式错误，必须为10位数字"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        return;
    }

    Member *member = find_member_by_card_id(card_id);
    if (member) {
		// 显示会员信息（包括剩余次数、卡类型和过期日期）
        char info[500];
        snprintf(info, sizeof(info),
            "════════ 会员信息 ════════\n"
            "姓名: %s\n"
            "卡类型: %s\n"
            "剩余次数: %d\n"
            "过期日期: %s\n"
            "═══════════════════════════",
            member->name,
            card_type_to_string(member->card_type),
            member->remaining_times,
            member->expiry_date);
		// 处理入场离场逻辑
        if (member->status == 0) {
            Record *records;
            int count;
            records = get_records_by_card_id(card_id, &count);
            
            int has_unclosed_entry = 0;
            if (count > 0) {
                for (int i = 0; i < count; i++) {
                    if (records[i].out_time[0] == '\0') {
                        has_unclosed_entry = 1;
                        break;
                    }
                }
                free(records);
            }
            
            if (has_unclosed_entry) {
                if (add_record(card_id, 0) == 0) {
                    char msg[200];
                    snprintf(msg, sizeof(msg), " %s 离场，感谢光临！\n剩余次数：%d", member->name, member->remaining_times);
                    GtkWidget *exit_dialog = gtk_message_dialog_new(
                        NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, 
                        GTK_BUTTONS_OK, "%s", msg
                    );
                    gtk_dialog_run(GTK_DIALOG(exit_dialog));
                    gtk_widget_destroy(exit_dialog);
                }
            } else {
				 // 入场处理 - 减少剩余次数（如果是次卡）
                if (member->card_type == CARD_TYPE_TIMES){
					if(member->remaining_times > 0) {
                    member->remaining_times--;
                    update_member(member);
                }else{
					GtkWidget *dialog = gtk_message_dialog_new(
                            NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, 
                            GTK_BUTTONS_OK, "次卡次数已用完，请充值"
                        );
                        gtk_dialog_run(GTK_DIALOG(dialog));
                        gtk_widget_destroy(dialog);
                        free(member);
                        return;
                    }
				}
                if (add_record(card_id, 1) == 0) {
                    char msg[200];
                    snprintf(msg, sizeof(msg), "欢迎 %s 光临！\n卡类型: %s\n剩余次数: %d\n过期日期: %s", member->name, card_type_to_string(member->card_type), member->remaining_times, member->expiry_date);
                    GtkWidget *entry_dialog = gtk_message_dialog_new(
                        NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, 
                        GTK_BUTTONS_OK, "%s", msg
                    );
                    gtk_dialog_run(GTK_DIALOG(entry_dialog));
                    gtk_widget_destroy(entry_dialog);
                }
            }
        }else{
			GtkWidget *dialog = gtk_message_dialog_new(
                NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, 
                GTK_BUTTONS_OK, "该卡已注销，无法使用"
            );
            gtk_dialog_run(GTK_DIALOG(dialog));
            gtk_widget_destroy(dialog);
        }
        free(member);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, 
            GTK_BUTTONS_OK, "该卡未注册，请进行注册"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 显示欢迎窗口（会员刷卡界面）
void show_welcome_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "乒乓球馆会员系统");
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 30);

    // 标题
    GtkWidget *title = gtk_label_new("会员请刷卡");
    GtkCssProvider *css_provider = gtk_css_provider_new();
    gtk_css_provider_load_from_data(css_provider,
        ".title-label {"
        "   font-size: 32px;"
        "   color: #333333;"
        "}",
        -1, NULL);

    GtkStyleContext *context = gtk_widget_get_style_context(title);
    gtk_style_context_add_provider(context,
        GTK_STYLE_PROVIDER(css_provider),
        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_style_context_add_class(context, "title-label");
    g_object_unref(css_provider);

    gtk_box_pack_start(GTK_BOX(vbox), title, TRUE, TRUE, 0);
    gtk_widget_set_halign(title, GTK_ALIGN_CENTER);
    gtk_widget_set_valign(title, GTK_ALIGN_CENTER);

	GtkWidget *hidden_card_entry = gtk_entry_new();
	gtk_entry_set_max_length(GTK_ENTRY(hidden_card_entry), 10);
	gtk_widget_set_visible(hidden_card_entry, FALSE);
	gtk_box_pack_start(GTK_BOX(vbox), hidden_card_entry, FALSE, FALSE, 0);

	g_signal_connect(hidden_card_entry, "changed", G_CALLBACK(on_card_input_changed), window);

    // 管理员登录按钮
    GtkWidget *admin_btn = gtk_button_new_with_label("管理员登录");
    gtk_widget_set_size_request(admin_btn, 120, 40);
    g_signal_connect(admin_btn, "clicked", G_CALLBACK(on_admin_login_clicked), window);
    gtk_box_pack_end(GTK_BOX(vbox), admin_btn, FALSE, FALSE, 10);

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
}

//卡号输入变化回调
static void on_card_input_changed(GtkWidget *entry, gpointer user_data){
	const char *card_id = gtk_entry_get_text(GTK_ENTRY(entry));
    if (strlen(card_id) == 10) { // 卡号为10位时触发
        process_card_scan(card_id);
        gtk_entry_set_text(GTK_ENTRY(entry), ""); // 清空输入框
    }
}

// 添加到代码中（例如在on_admin_login_clicked函数之前）
GtkWidget* create_login_window(GtkWidget *parent) {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "管理员登录");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));  // 设为父窗口的子窗口

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);

    // 用户名输入
    GtkWidget *user_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), user_hbox, FALSE, FALSE, 5);
    GtkWidget *user_label = gtk_label_new("用户名:");
    GtkWidget *user_entry = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(user_hbox), user_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(user_hbox), user_entry, TRUE, TRUE, 5);

    // 密码输入
    GtkWidget *pass_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), pass_hbox, FALSE, FALSE, 5);
    GtkWidget *pass_label = gtk_label_new("密码:");
    GtkWidget *pass_entry = gtk_entry_new();
    gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);  // 密码隐藏
    g_object_set_data(G_OBJECT(user_entry), "password", pass_entry);  // 关联密码输入框
    gtk_box_pack_start(GTK_BOX(pass_hbox), pass_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(pass_hbox), pass_entry, TRUE, TRUE, 5);

    // 登录按钮
    GtkWidget *login_btn = gtk_button_new_with_label("登录");
    g_signal_connect(login_btn, "clicked", G_CALLBACK(login_clicked), user_entry);
    gtk_box_pack_start(GTK_BOX(vbox), login_btn, FALSE, FALSE, 10);

	gtk_widget_show_all(window);

    return window;
}

// 管理员登录按钮回调
static void on_admin_login_clicked(GtkWidget *widget, gpointer data) {
	GtkWidget *welcome_window = GTK_WIDGET(data);
    GtkWidget *login_window = create_login_window(welcome_window);
    gtk_widget_hide(welcome_window);
    g_signal_connect(login_window, "destroy", G_CALLBACK(gtk_widget_show), welcome_window);
}


// 登录按钮回调函数
static void login_clicked(GtkWidget *widget, gpointer data) {
    const char *username = gtk_entry_get_text(GTK_ENTRY(data));
    const char *password = gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(data), "password")));

    int login_result = admin_login(username, password);
    if (login_result) {
        strncpy(current_admin, username, sizeof(current_admin) - 1);
        current_admin[sizeof(current_admin) - 1] = '\0';
        
        GtkWidget *login_window = gtk_widget_get_toplevel(widget);
        gtk_widget_hide(login_window);
        
        create_main_window();
        
        g_timeout_add(100, (GSourceFunc)gtk_widget_destroy, login_window);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(gtk_widget_get_toplevel(widget)), 
            GTK_DIALOG_MODAL, 
            GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, 
            "用户名或者密码错误"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}
// 更新主窗口创建函数，使用CSS替代已弃用的字体函数
void create_main_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "乒乓球馆会员管理系统");
    gtk_window_set_default_size(GTK_WINDOW(window), 1000, 900);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);

    // 标题 - 使用CSS设置字体样式（替代已弃用的函数）
    GtkWidget *title = gtk_label_new("会员管理系统主菜单");
    
    // 创建CSS提供者设置字体
    GtkCssProvider *css_provider = gtk_css_provider_new();
    gtk_css_provider_load_from_data(css_provider,
        "label.title-label {"
        "   font-size: 24px;"
        "   font-weight: bold;"
        "}",
        -1, NULL);
    
    // 应用CSS样式
    GtkStyleContext *context = gtk_widget_get_style_context(title);
    gtk_style_context_add_provider(context,
        GTK_STYLE_PROVIDER(css_provider),
        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_style_context_add_class(context, "title-label");
    
    // 释放资源
    g_object_unref(css_provider);

    gtk_box_pack_start(GTK_BOX(vbox), title, FALSE, FALSE, 20);
    gtk_widget_set_halign(title, GTK_ALIGN_CENTER);

    // 功能按钮（保持不变）
    GtkWidget *grid = gtk_grid_new();
    gtk_grid_set_row_spacing(GTK_GRID(grid), 20);
    gtk_grid_set_column_spacing(GTK_GRID(grid), 20);
    gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
    gtk_box_pack_start(GTK_BOX(vbox), grid, TRUE, TRUE, 10);

    GtkWidget *reg_btn = gtk_button_new_with_label("会员注册");
    gtk_widget_set_size_request(reg_btn, 150, 50);
    g_signal_connect(reg_btn, "clicked", G_CALLBACK(create_register_window), NULL);
    gtk_grid_attach(GTK_GRID(grid), reg_btn, 0, 0, 1, 1);

    GtkWidget *query_btn = gtk_button_new_with_label("会员查询");
    gtk_widget_set_size_request(query_btn, 150, 50);
    g_signal_connect(query_btn, "clicked", G_CALLBACK(create_query_window), NULL);
    gtk_grid_attach(GTK_GRID(grid), query_btn, 1, 0, 1, 1);


    GtkWidget *manage_btn = gtk_button_new_with_label("会员管理");
    gtk_widget_set_size_request(manage_btn, 150, 50);
    g_signal_connect(manage_btn, "clicked", G_CALLBACK(create_member_manage_window), NULL);
    gtk_grid_attach(GTK_GRID(grid), manage_btn, 0, 2, 1, 1);

    GtkWidget *record_btn = gtk_button_new_with_label("记录查询");
    gtk_widget_set_size_request(record_btn, 150, 50);
    g_signal_connect(record_btn, "clicked", G_CALLBACK(create_record_window), NULL);
    gtk_grid_attach(GTK_GRID(grid), record_btn, 1, 2, 1, 1);

    // 返回会员刷卡界面按钮
    GtkWidget *back_btn = gtk_button_new_with_label("返回会员刷卡界面");
    gtk_widget_set_size_request(back_btn, 150, 50);
    g_signal_connect(back_btn, "clicked", G_CALLBACK(show_welcome_window), window);
    gtk_grid_attach(GTK_GRID(grid), back_btn, 0, 3, 2, 1);

    // 状态栏显示当前登录用户
    GtkWidget *status_bar = gtk_statusbar_new();
    guint context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar), "status");
    gtk_statusbar_push(GTK_STATUSBAR(status_bar), context_id, g_strdup_printf("当前登录: %s", current_admin));
    gtk_box_pack_end(GTK_BOX(vbox), status_bar, FALSE, FALSE, 0);

    // 确保主窗口显示
    gtk_widget_show_all(window);
}


// 会员注册窗口
void create_register_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "会员注册");
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 500);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);

    // 卡号输入
    GtkWidget *card_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), card_hbox, FALSE, FALSE, 5);
    GtkWidget *card_label = gtk_label_new("卡号 (10位):");
    GtkWidget *card_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(card_entry), 10);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_entry, TRUE, TRUE, 5);

    // 姓名输入
    GtkWidget *name_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), name_hbox, FALSE, FALSE, 5);
    GtkWidget *name_label = gtk_label_new("姓名:");
    GtkWidget *name_entry = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(name_hbox), name_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_entry, TRUE, TRUE, 5);

	 // 性别选择
    GtkWidget *gender_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), gender_hbox, FALSE, FALSE, 5);
    GtkWidget *gender_label = gtk_label_new("性别:");
    GtkWidget *gender_combo = gtk_combo_box_text_new();
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "男");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "女");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "其他");
    gtk_combo_box_set_active(GTK_COMBO_BOX(gender_combo), 0);
    gtk_box_pack_start(GTK_BOX(gender_hbox), gender_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(gender_hbox), gender_combo, TRUE, TRUE, 5);

	// 电话输入
	GtkWidget *phone_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
	gtk_box_pack_start(GTK_BOX(vbox), phone_hbox, FALSE, FALSE, 5);
	GtkWidget *phone_label = gtk_label_new("电话:");
	GtkWidget *phone_entry = gtk_entry_new();  // 这是缺失的变量
	gtk_box_pack_start(GTK_BOX(phone_hbox), phone_label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(phone_hbox), phone_entry, TRUE, TRUE, 5);
	// 同时在g_object_set_data中添加：
	g_object_set_data(G_OBJECT(window), "phone_entry", phone_entry);
	
	// 生日选择（使用GtkCalendar）
	GtkWidget *birthday_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
	gtk_box_pack_start(GTK_BOX(vbox), birthday_hbox, FALSE, FALSE, 5);
	GtkWidget *birthday_label = gtk_label_new("生日:");
	GtkWidget *birthday_calendar = gtk_calendar_new(); // 直接创建日历组件
	// 设置默认日期（年，月（0-11），日）
	gtk_calendar_select_month(GTK_CALENDAR(birthday_calendar), 0, 2000); // 2000年1月
	gtk_calendar_select_day(GTK_CALENDAR(birthday_calendar), 1); // 1日
	gtk_box_pack_start(GTK_BOX(birthday_hbox), birthday_label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(birthday_hbox), birthday_calendar, TRUE, TRUE, 5);
	g_object_set_data(G_OBJECT(window), "birthday_calendar", birthday_calendar);

    // 卡类型选择
    GtkWidget *type_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), type_hbox, FALSE, FALSE, 5);
    GtkWidget *type_label = gtk_label_new("卡类型:");
    GtkWidget *type_combo = gtk_combo_box_text_new();
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "次卡");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "月卡");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "季卡");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "年卡");
    gtk_combo_box_set_active(GTK_COMBO_BOX(type_combo), 0);
    gtk_box_pack_start(GTK_BOX(type_hbox), type_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(type_hbox), type_combo, TRUE, TRUE, 5);

    // 剩余次数
    GtkWidget *times_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), times_hbox, FALSE, FALSE, 5);
    GtkWidget *times_label = gtk_label_new("剩余次数:");
    GtkWidget *times_entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(times_entry), "0");
    gtk_box_pack_start(GTK_BOX(times_hbox), times_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(times_hbox), times_entry, TRUE, TRUE, 5);

	// 过期日期选择（使用GtkCalendar）
	GtkWidget *expiry_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
	gtk_box_pack_start(GTK_BOX(vbox), expiry_hbox, FALSE, FALSE, 5);
	GtkWidget *expiry_label = gtk_label_new("过期日期:");
	GtkWidget *expiry_calendar = gtk_calendar_new();
	// 设置默认日期为1年后
	time_t now = time(NULL);
	struct tm *tm = localtime(&now);
	tm->tm_year += 1; // 年份+1
	gtk_calendar_select_month(GTK_CALENDAR(expiry_calendar), tm->tm_mon, tm->tm_year + 1900);
	gtk_calendar_select_day(GTK_CALENDAR(expiry_calendar), tm->tm_mday);
	gtk_box_pack_start(GTK_BOX(expiry_hbox), expiry_label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(expiry_hbox), expiry_calendar, TRUE, TRUE, 5);
	g_object_set_data(G_OBJECT(window), "expiry_calendar", expiry_calendar);

    // 按钮区域 - 移除读取卡号按钮
    GtkWidget *btn_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
    gtk_box_pack_start(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 20);

    // 保存按钮
    GtkWidget *save_btn = gtk_button_new_with_label("保存");
    g_signal_connect(save_btn, "clicked", G_CALLBACK(register_save_clicked), window);
    // 修正：存储控件数据（移除birthday_entry和expiry_entry）
    g_object_set_data(G_OBJECT(window), "card_entry", card_entry);
    g_object_set_data(G_OBJECT(window), "name_entry", name_entry);
    g_object_set_data(G_OBJECT(window), "gender_combo", gender_combo);
    g_object_set_data(G_OBJECT(window), "birthday_calendar", birthday_calendar);
    g_object_set_data(G_OBJECT(window), "phone_entry", phone_entry);
    g_object_set_data(G_OBJECT(window), "type_combo", type_combo);
    g_object_set_data(G_OBJECT(window), "times_entry", times_entry);
    g_object_set_data(G_OBJECT(window), "expiry_calendar", expiry_calendar);
    gtk_box_pack_start(GTK_BOX(btn_hbox), save_btn, TRUE, TRUE, 5);

    // 取消按钮
    GtkWidget *cancel_btn = gtk_button_new_with_label("取消");
    g_signal_connect(cancel_btn, "clicked", G_CALLBACK(gtk_widget_destroy), window);
    gtk_box_pack_start(GTK_BOX(btn_hbox), cancel_btn, TRUE, TRUE, 5);

    gtk_widget_show_all(window);
}


// 注册保存按钮回调
static void register_save_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    
    GtkWidget *card_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "card_entry"));
    GtkWidget *name_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "name_entry"));
    GtkWidget *gender_combo = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "gender_combo"));
    GtkWidget *phone_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "phone_entry"));
    GtkWidget *type_combo = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "type_combo"));
    GtkWidget *times_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "times_entry"));
    
    const char *card_id = gtk_entry_get_text(GTK_ENTRY(card_entry));
    const char *name = gtk_entry_get_text(GTK_ENTRY(name_entry));
    const char *gender_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(gender_combo));

	const char *phone = gtk_entry_get_text(GTK_ENTRY(phone_entry));
    const char *type_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(type_combo));
    const char *times_str = gtk_entry_get_text(GTK_ENTRY(times_entry));

	// 处理生日（从GtkCalendar获取）
	GtkWidget *birthday_calendar = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "birthday_calendar"));
	guint birth_year, birth_month, birth_day;
	gtk_calendar_get_date(GTK_CALENDAR(birthday_calendar), &birth_year, &birth_month, &birth_day);
	char birthday_str[13];
	// 注意：month是0-11，需要+1转为1-12
	snprintf(birthday_str, sizeof(birthday_str), "%d-%02d-%02d", birth_year, birth_month + 1, birth_day);

	// 处理过期日期（从GtkCalendar获取）
	GtkWidget *expiry_calendar = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "expiry_calendar"));
	guint exp_year, exp_month, exp_day;
	gtk_calendar_get_date(GTK_CALENDAR(expiry_calendar), &exp_year, &exp_month, &exp_day);
	char expiry_str[13];
	snprintf(expiry_str, sizeof(expiry_str), "%d-%02d-%02d", exp_year, exp_month + 1, exp_day);

	Member member;
    memset(&member, 0, sizeof(Member));
	
	// 验证卡号长度
    if (strlen(card_id) != 10) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "卡号必须为10位数字"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        return;
    }
    
    // 验证必填字段
    if (strlen(name) == 0 || strlen(phone) == 0 || strlen(birthday_str) == 0 || strlen(expiry_str) == 0) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "请填写完整信息"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        return;
    }
    
    // 创建会员对象
    strncpy(member.card_id, card_id, sizeof(member.card_id)-1);
    strncpy(member.name, name, sizeof(member.name)-1);
    
    if (strcmp(gender_str, "男") == 0) member.gender = GENDER_MALE;
    else if (strcmp(gender_str, "女") == 0) member.gender = GENDER_FEMALE;
    else member.gender = GENDER_OTHER;
    
    strncpy(member.birthday, birthday_str, sizeof(member.birthday)-1);
    strncpy(member.phone, phone, sizeof(member.phone)-1);
    member.card_type = string_to_card_type(type_str);
    member.remaining_times = atoi(times_str);
    strncpy(member.expiry_date, expiry_str, sizeof(member.expiry_date)-1);
    member.status = 0; // 正常状态
    
    // 注册时间会在add_member函数中自动设置
    
    // 保存会员
    int result = add_member(&member);
    if (result == 0) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, 
            GTK_BUTTONS_OK, "会员注册成功"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        gtk_widget_destroy(window);
    } else if (result == -2) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "卡号已存在，无法注册"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "注册失败，请重试"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 显示会员信息对话框
void show_member_info_dialog(GtkWidget *parent, Member *member) {
    if (!member) return;

    const char *gender_str = "";
    switch(member->gender) {
        case GENDER_MALE: gender_str = "男"; break;
        case GENDER_FEMALE: gender_str = "女"; break;
        default: gender_str = "其他"; break;
    }

    char info[700];
    snprintf(info, sizeof(info),
        "════════ 会员信息 ════════\n"
        "卡号: %s\n"
        "姓名: %s\n"
        "性别: %s\n"
        "生日: %s\n"
        "电话: %s\n"
        "注册时间: %s\n"
        "卡类型: %s\n"
        "剩余次数: %d\n"
        "过期日期: %s\n"
        "状态: %s\n"
        "═══════════════════════════",
        member->card_id,
        member->name,
        gender_str,
        member->birthday,
        member->phone,
        member->register_date,
        card_type_to_string(member->card_type),
        member->remaining_times,
        member->expiry_date,
        member->status == 0 ? "正常" : "已注销"
    );

    GtkWidget *dialog = gtk_message_dialog_new(
        GTK_WINDOW(parent), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
        GTK_BUTTONS_OK, "%s", info
    );
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}
// 创建会员查询窗口
void create_query_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "会员查询");
    gtk_window_set_default_size(GTK_WINDOW(window), 1000, 600);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);

    // 查询条件区域
    GtkWidget *query_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), query_hbox, FALSE, FALSE, 5);

    // 卡号查询
    GtkWidget *card_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), card_hbox, TRUE, TRUE, 5);
    GtkWidget *card_label = gtk_label_new("按卡号:");
    GtkWidget *card_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(card_entry), 10);
    GtkWidget *card_btn = gtk_button_new_with_label("查询");
    g_signal_connect(card_btn, "clicked", G_CALLBACK(query_by_card_clicked), window);
    g_object_set_data(G_OBJECT(window), "card_entry", card_entry);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_btn, FALSE, FALSE, 5);

    // 姓名查询
    GtkWidget *name_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), name_hbox, TRUE, TRUE, 5);
    GtkWidget *name_label = gtk_label_new("按姓名:");
    GtkWidget *name_entry = gtk_entry_new();
    GtkWidget *name_btn = gtk_button_new_with_label("查询");
    g_signal_connect(name_btn, "clicked", G_CALLBACK(query_by_name_clicked), window);
    g_object_set_data(G_OBJECT(window), "name_entry", name_entry);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_btn, FALSE, FALSE, 5);

    // 结果显示区域
    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 5);

	// 修改列表存储定义，增加列数
	GtkListStore *store = gtk_list_store_new(9, 
    	G_TYPE_STRING,  // 0:卡号
    	G_TYPE_STRING,  // 1:姓名
    	G_TYPE_STRING,  // 2:性别
    	G_TYPE_STRING,  // 3:生日
    	G_TYPE_STRING,  // 4:电话
    	G_TYPE_STRING,  // 5:卡类型
    	G_TYPE_INT,     // 6:剩余次数
    	G_TYPE_STRING,  // 7:注册日期
    	G_TYPE_STRING   // 8:状态
	);

	GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    g_object_unref(store);

    // 添加列前先创建渲染器和列变量
    GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
    GtkTreeViewColumn *column;


	// 修改列定义（确保列顺序与存储一致）
	column = gtk_tree_view_column_new_with_attributes("卡号", renderer, "text", 0, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("姓名", renderer, "text", 1, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("性别", renderer, "text", 2, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("生日", renderer, "text", 3, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("电话", renderer, "text", 4, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("卡类型", renderer, "text", 5, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("剩余次数", renderer, "text", 6, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("注册日期", renderer, "text", 7, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

	column = gtk_tree_view_column_new_with_attributes("状态", renderer, "text", 8, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    gtk_container_add(GTK_CONTAINER(scrolled_window), treeview);
    g_object_set_data(G_OBJECT(window), "treeview", treeview);

    // 刷新按钮
    GtkWidget *refresh_btn = gtk_button_new_with_label("刷新");
    g_signal_connect(refresh_btn, "clicked", G_CALLBACK(on_refresh_clicked), treeview);
    gtk_box_pack_start(GTK_BOX(vbox), refresh_btn, FALSE, FALSE, 5);

    // 初始加载数据
    refresh_member_list(treeview);

    gtk_widget_show_all(window);
}


// 按卡号查询回调
static void query_by_card_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    GtkWidget *card_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "card_entry"));
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "treeview"));
    const char *card_id = gtk_entry_get_text(GTK_ENTRY(card_entry));

    // 检查卡号长度
    if (strlen(card_id) > 0 && strlen(card_id) != 10) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "卡号必须为10位数字"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        return;
    }

    // 清空现有数据
    GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)));
    gtk_list_store_clear(store);

    // 查询会员
    Member *member = find_member_by_card_id(card_id);
	  if (member) {
        const char *gender_str;
        switch(member->gender) {
            case GENDER_MALE: gender_str = "男"; break;
            case GENDER_FEMALE: gender_str = "女"; break;
            default: gender_str = "其他"; break;
        }

        GtkTreeIter iter;
        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter,
            0, member->card_id,
            1, member->name,
            2, gender_str,
            3, member->birthday,
            4, member->phone,
            5, card_type_to_string(member->card_type),
            6, member->remaining_times,
            7, member->register_date,
            8, member->status == 0 ? "正常" : "已注销",
            -1);
        free(member);
    }else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, 
            GTK_BUTTONS_OK, "未找到该会员"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 按姓名查询回调
static void query_by_name_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    GtkWidget *name_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "name_entry"));
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "treeview"));
    const char *name = gtk_entry_get_text(GTK_ENTRY(name_entry));

    if (strlen(name) == 0) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, 
            GTK_BUTTONS_OK, "请输入姓名"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        return;
    }

    // 清空现有数据
    GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)));
    gtk_list_store_clear(store);

    // 查询会员
    int count;
    Member *members = find_member_by_name(name, &count);
	 if (members && count > 0) {
        for (int i = 0; i < count; i++) {
            const char *gender_str;
            switch(members[i].gender) {
                case GENDER_MALE: gender_str = "男"; break;
                case GENDER_FEMALE: gender_str = "女"; break;
                default: gender_str = "其他"; break;
            }

            GtkTreeIter iter;
            gtk_list_store_append(store, &iter);
            gtk_list_store_set(store, &iter,
                0, members[i].card_id,
                1, members[i].name,
                2, gender_str,
                3, members[i].birthday,
                4, members[i].phone,
                5, card_type_to_string(members[i].card_type),
                6, members[i].remaining_times,
                7, members[i].register_date,
                8, members[i].status == 0 ? "正常" : "已注销",
                -1);
        }
        free(members);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, 
            GTK_BUTTONS_OK, "未找到相关会员"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 刷新列表回调
static void on_refresh_clicked(GtkWidget *w, gpointer d) {
    refresh_member_list(GTK_WIDGET(d));
}

// 刷新会员列表
void refresh_member_list(GtkWidget *treeview) {
    GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)));
    gtk_list_store_clear(store);

    int count;
    Member *members = get_all_members(&count);
    if (members && count > 0) {
        for (int i = 0; i < count; i++) {
            // 处理性别字符串
            const char *gender_str;
            switch(members[i].gender) {
                case GENDER_MALE: gender_str = "男"; break;
                case GENDER_FEMALE: gender_str = "女"; break;
                default: gender_str = "其他"; break;
            }

            GtkTreeIter iter;
            gtk_list_store_append(store, &iter);
            gtk_list_store_set(store, &iter,
                0, members[i].card_id,
                1, members[i].name,
                2, gender_str,
                3, members[i].birthday,
                4, members[i].phone,
                5, card_type_to_string(members[i].card_type),
                6, members[i].remaining_times,
                7, members[i].register_date,
                8, members[i].status == 0 ? "正常" : "已注销",
                -1);
        }
        free(members);
    }
}
// 创建会员管理窗口
void create_member_manage_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "会员管理");
    gtk_window_set_default_size(GTK_WINDOW(window), 900, 600);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
	
	// 修改列表存储定义，增加列数
	GtkListStore *store = gtk_list_store_new(9, 
    	G_TYPE_STRING,  // 0:卡号
    	G_TYPE_STRING,  // 1:姓名
    	G_TYPE_STRING,  // 2:性别
    	G_TYPE_STRING,  // 3:生日
    	G_TYPE_STRING,  // 4:电话
    	G_TYPE_STRING,  // 5:卡类型
    	G_TYPE_INT,     // 6:剩余次数
    	G_TYPE_STRING,  // 7:注册日期
    	G_TYPE_STRING   // 8:状态
	);


	GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    g_object_unref(store);
	

    // 设置选择模式
    gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),
                               GTK_SELECTION_SINGLE);

	
	
	 // 在滚动窗口前添加查询条件区域
    GtkWidget *query_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), query_hbox, FALSE, FALSE, 5);

    // 卡号查询
    GtkWidget *card_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), card_hbox, TRUE, TRUE, 5);
    GtkWidget *card_label = gtk_label_new("按卡号:");
    GtkWidget *card_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(card_entry), 10);
    GtkWidget *card_btn = gtk_button_new_with_label("查询");
    g_signal_connect(card_btn, "clicked", G_CALLBACK(query_by_card_clicked), window);
    g_object_set_data(G_OBJECT(window), "card_entry", card_entry);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(card_hbox), card_btn, FALSE, FALSE, 5);

    // 姓名查询
    GtkWidget *name_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), name_hbox, TRUE, TRUE, 5);
    GtkWidget *name_label = gtk_label_new("按姓名:");
    GtkWidget *name_entry = gtk_entry_new();
    GtkWidget *name_btn = gtk_button_new_with_label("查询");
    g_signal_connect(name_btn, "clicked", G_CALLBACK(query_by_name_clicked), window);
    g_object_set_data(G_OBJECT(window), "name_entry", name_entry);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(name_hbox), name_btn, FALSE, FALSE, 5);

    // 刷新按钮移到查询区域
    GtkWidget *refresh_btn = gtk_button_new_with_label("刷新列表");
    g_signal_connect(refresh_btn, "clicked", G_CALLBACK(on_refresh_clicked), treeview);
    gtk_box_pack_start(GTK_BOX(query_hbox), refresh_btn, FALSE, FALSE, 5);

	 // 创建滚动窗口
    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);


    // 创建渲染器
    GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
	  // 添加列
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("卡号", renderer, "text", 0, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("姓名", renderer, "text", 1, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("性别", renderer, "text", 2, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("生日", renderer, "text", 3, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("电话", renderer, "text", 4, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("卡类型", renderer, "text", 5, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("剩余次数", renderer, "text", 6, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("注册日期", renderer, "text", 7, NULL));
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),
        gtk_tree_view_column_new_with_attributes("状态", renderer, "text", 8, NULL));

    // 添加树视图到滚动窗口
    gtk_container_add(GTK_CONTAINER(scrolled_window), treeview);
    g_object_set_data(G_OBJECT(window), "treeview", treeview);

    // 按钮区域
    GtkWidget *button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_END);
    gtk_box_pack_end(GTK_BOX(vbox), button_box, FALSE, FALSE, 5);


    // 修改按钮
    GtkWidget *modify_btn = gtk_button_new_with_label("修改会员");
    g_signal_connect(modify_btn, "clicked", G_CALLBACK(modify_member_clicked), window);
    gtk_container_add(GTK_CONTAINER(button_box), modify_btn);

    // 注销按钮
    GtkWidget *deactivate_btn = gtk_button_new_with_label("注销会员");
    g_signal_connect(deactivate_btn, "clicked", G_CALLBACK(manage_deactivate_clicked), window);
    gtk_container_add(GTK_CONTAINER(button_box), deactivate_btn);

    // 加载初始数据
    refresh_member_list(treeview);

    // 显示所有控件
    gtk_widget_show_all(window);
}


// 注销会员回调
static void manage_deactivate_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "treeview"));
    
    // 获取选中的行
    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    GtkTreeModel *model;
    GtkTreeIter iter;
    
    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
        // 获取卡号
        gchar *card_id;
        gtk_tree_model_get(model, &iter, 0, &card_id, -1);
        
        // 确认对话框
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_YES_NO, "确定要注销卡号为 %s 的会员吗？", card_id
        );
        
        gint response = gtk_dialog_run(GTK_DIALOG(dialog));
        if (response == GTK_RESPONSE_YES) {
            // 先获取会员信息
            Member *member = find_member_by_card_id(card_id);
            if (member) {
                member->status = 1; // 设置为注销状态
                int result = update_member(member);
                free(member);
                
                if (result == 0) {
                    GtkWidget *info_dialog = gtk_message_dialog_new(
                        GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
                        GTK_BUTTONS_OK, "会员注销成功"
                    );
                    gtk_dialog_run(GTK_DIALOG(info_dialog));
                    gtk_widget_destroy(info_dialog);
                    // 刷新列表
                    refresh_member_list(treeview);
                } else {
                    GtkWidget *error_dialog = gtk_message_dialog_new(
                        GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
                        GTK_BUTTONS_OK, "会员注销失败"
                    );
                    gtk_dialog_run(GTK_DIALOG(error_dialog));
                    gtk_widget_destroy(error_dialog);
                }
            }
        }
        
        g_free(card_id);
        gtk_widget_destroy(dialog);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK, "请先选择要注销的会员"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}
static void save_member_changes(GtkWidget *widget, gpointer data) {
    GtkWidget *modify_window = GTK_WIDGET(data);
    GtkWidget *parent_window = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "parent_window"));
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(parent_window), "treeview"));

    // 获取输入控件
    GtkWidget *card_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "card_entry"));
    GtkWidget *name_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "name_entry"));
	 GtkWidget *gender_combo = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "gender_combo"));
    GtkWidget *birthday_calendar = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "birthday_calendar"));
    GtkWidget *phone_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "phone_entry"));
    GtkWidget *type_combo = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "type_combo"));
    GtkWidget *times_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "times_entry"));
    GtkWidget *expiry_calendar = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "expiry_calendar"));
    GtkWidget *status_combo = GTK_WIDGET(g_object_get_data(G_OBJECT(modify_window), "status_combo"));

    // 获取输入值
    const char *card_id = gtk_entry_get_text(GTK_ENTRY(card_entry));
    const char *name = gtk_entry_get_text(GTK_ENTRY(name_entry));
    const char *phone = gtk_entry_get_text(GTK_ENTRY(phone_entry));
    const char *times_str = gtk_entry_get_text(GTK_ENTRY(times_entry));
    int remaining_times = atoi(times_str);

	 // 获取性别
    const char *gender_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(gender_combo));
    Gender gender;
    if (strcmp(gender_str, "男") == 0) gender = GENDER_MALE;
    else if (strcmp(gender_str, "女") == 0) gender = GENDER_FEMALE;
    else gender = GENDER_OTHER;

    // 获取生日
    guint birth_year, birth_month, birth_day;
    gtk_calendar_get_date(GTK_CALENDAR(birthday_calendar), &birth_year, &birth_month, &birth_day);
    char birthday_str[13];
    snprintf(birthday_str, sizeof(birthday_str), "%d-%02d-%02d", birth_year, birth_month + 1, birth_day);

    // 获取过期日期
    guint exp_year, exp_month, exp_day;
    gtk_calendar_get_date(GTK_CALENDAR(expiry_calendar), &exp_year, &exp_month, &exp_day);
    char expiry_str[13];
    snprintf(expiry_str, sizeof(expiry_str), "%d-%02d-%02d", exp_year, exp_month + 1, exp_day);

    CardType card_type = gtk_combo_box_get_active(GTK_COMBO_BOX(type_combo));
    int status = gtk_combo_box_get_active(GTK_COMBO_BOX(status_combo));

    // 创建会员对象
    Member member;
    strncpy(member.card_id, card_id, sizeof(member.card_id));
    strncpy(member.name, name, sizeof(member.name));
    strncpy(member.phone, phone, sizeof(member.phone));
	strncpy(member.birthday, birthday_str, sizeof(member.birthday));
    strncpy(member.expiry_date, expiry_str, sizeof(member.expiry_date));
	member.gender = gender;
    member.card_type = card_type;
    member.remaining_times = remaining_times;
    member.status = status;

	// 获取原始会员信息以保留注册时间
    Member *original_member = find_member_by_card_id(card_id);
    if (original_member) {
        strncpy(member.register_date, original_member->register_date, sizeof(member.register_date));
        free(original_member);
    } else {
        get_current_time(member.register_date, 0);
    }

    // 更新会员信息
    if (update_member(&member) == 0) {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(modify_window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK, "会员信息更新成功"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);

		 refresh_member_list(treeview);

		 gtk_widget_destroy(modify_window);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(modify_window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
            GTK_BUTTONS_OK, "更新会员信息失败"
        );

        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 修改会员信息回调
static void modify_member_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "treeview"));
    
    // 获取选中的行
    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    GtkTreeModel *model;
    GtkTreeIter iter;
    
    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
        // 获取卡号
        gchar *card_id;
        gtk_tree_model_get(model, &iter, 0, &card_id, -1);
        
        // 查找会员信息
        Member *member = find_member_by_card_id(card_id);
        if (member) {
            // 创建修改窗口
            GtkWidget *modify_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
            gtk_window_set_title(GTK_WINDOW(modify_window), "修改会员信息");
            gtk_window_set_default_size(GTK_WINDOW(modify_window), 600, 500);
            gtk_window_set_position(GTK_WINDOW(modify_window), GTK_WIN_POS_CENTER);

            GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15);
            gtk_container_add(GTK_CONTAINER(modify_window), vbox);
            gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);

            // 卡号输入（不可修改）
            GtkWidget *card_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), card_hbox, FALSE, FALSE, 5);
            GtkWidget *card_label = gtk_label_new("卡号:");
            GtkWidget *card_entry = gtk_entry_new();
            gtk_entry_set_text(GTK_ENTRY(card_entry), member->card_id);
            gtk_editable_set_editable(GTK_EDITABLE(card_entry), FALSE); // 不可编辑
            gtk_box_pack_start(GTK_BOX(card_hbox), card_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(card_hbox), card_entry, TRUE, TRUE, 5);

            // 姓名输入
            GtkWidget *name_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), name_hbox, FALSE, FALSE, 5);
            GtkWidget *name_label = gtk_label_new("姓名:");
            GtkWidget *name_entry = gtk_entry_new();
            gtk_entry_set_text(GTK_ENTRY(name_entry), member->name);
            gtk_box_pack_start(GTK_BOX(name_hbox), name_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(name_hbox), name_entry, TRUE, TRUE, 5);

			  // 性别选择
            GtkWidget *gender_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), gender_hbox, FALSE, FALSE, 5);
            GtkWidget *gender_label = gtk_label_new("性别:");
            GtkWidget *gender_combo = gtk_combo_box_text_new();
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "男");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "女");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(gender_combo), "其他");
            // 设置当前性别
            switch(member->gender) {
                case GENDER_MALE: gtk_combo_box_set_active(GTK_COMBO_BOX(gender_combo), 0); break;
                case GENDER_FEMALE: gtk_combo_box_set_active(GTK_COMBO_BOX(gender_combo), 1); break;
                default: gtk_combo_box_set_active(GTK_COMBO_BOX(gender_combo), 2); break;
            }
            gtk_box_pack_start(GTK_BOX(gender_hbox), gender_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(gender_hbox), gender_combo, TRUE, TRUE, 5);

			// 生日选择（使用GtkCalendar）
            GtkWidget *birthday_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), birthday_hbox, FALSE, FALSE, 5);
            GtkWidget *birthday_label = gtk_label_new("生日:");
            GtkWidget *birthday_calendar = gtk_calendar_new();
            // 解析当前生日并设置日历
            int year, month, day;
            if (sscanf(member->birthday, "%d-%d-%d", &year, &month, &day) == 3) {
                gtk_calendar_select_month(GTK_CALENDAR(birthday_calendar), month - 1, year);
                gtk_calendar_select_day(GTK_CALENDAR(birthday_calendar), day);
            }
            gtk_box_pack_start(GTK_BOX(birthday_hbox), birthday_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(birthday_hbox), birthday_calendar, TRUE, TRUE, 5);

            // 电话输入
            GtkWidget *phone_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), phone_hbox, FALSE, FALSE, 5);
            GtkWidget *phone_label = gtk_label_new("电话:");
            GtkWidget *phone_entry = gtk_entry_new();
            gtk_entry_set_text(GTK_ENTRY(phone_entry), member->phone);
            gtk_box_pack_start(GTK_BOX(phone_hbox), phone_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(phone_hbox), phone_entry, TRUE, TRUE, 5);

            // 卡类型选择
            GtkWidget *type_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), type_hbox, FALSE, FALSE, 5);
            GtkWidget *type_label = gtk_label_new("卡类型:");
            GtkWidget *type_combo = gtk_combo_box_text_new();
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "次卡");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "月卡");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "季卡");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(type_combo), "年卡");
            gtk_combo_box_set_active(GTK_COMBO_BOX(type_combo), member->card_type);
            gtk_box_pack_start(GTK_BOX(type_hbox), type_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(type_hbox), type_combo, TRUE, TRUE, 5);

            // 剩余次数
            GtkWidget *times_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), times_hbox, FALSE, FALSE, 5);
            GtkWidget *times_label = gtk_label_new("剩余次数:");
            GtkWidget *times_entry = gtk_entry_new();
            char times_str[20];
            snprintf(times_str, sizeof(times_str), "%d", member->remaining_times);
            gtk_entry_set_text(GTK_ENTRY(times_entry), times_str);
            gtk_box_pack_start(GTK_BOX(times_hbox), times_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(times_hbox), times_entry, TRUE, TRUE, 5);

			// 过期日期选择（使用GtkCalendar）
            GtkWidget *expiry_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), expiry_hbox, FALSE, FALSE, 5);
            GtkWidget *expiry_label = gtk_label_new("过期日期:");
            GtkWidget *expiry_calendar = gtk_calendar_new();
            // 解析当前过期日期并设置日历
            if (sscanf(member->expiry_date, "%d-%d-%d", &year, &month, &day) == 3) {
                gtk_calendar_select_month(GTK_CALENDAR(expiry_calendar), month - 1, year);
                gtk_calendar_select_day(GTK_CALENDAR(expiry_calendar), day);
            }
            gtk_box_pack_start(GTK_BOX(expiry_hbox), expiry_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(expiry_hbox), expiry_calendar, TRUE, TRUE, 5);

            // 会员状态
            GtkWidget *status_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
            gtk_box_pack_start(GTK_BOX(vbox), status_hbox, FALSE, FALSE, 5);
            GtkWidget *status_label = gtk_label_new("会员状态:");
            GtkWidget *status_combo = gtk_combo_box_text_new();
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(status_combo), "正常");
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(status_combo), "已注销");
            gtk_combo_box_set_active(GTK_COMBO_BOX(status_combo), member->status);
            gtk_box_pack_start(GTK_BOX(status_hbox), status_label, FALSE, FALSE, 5);
            gtk_box_pack_start(GTK_BOX(status_hbox), status_combo, TRUE, TRUE, 5);

            // 按钮区域
            GtkWidget *btn_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
            gtk_box_pack_start(GTK_BOX(vbox), btn_hbox, FALSE, FALSE, 20);

            // 保存按钮
            GtkWidget *save_btn = gtk_button_new_with_label("保存");
            g_signal_connect(save_btn, "clicked", G_CALLBACK(save_member_changes), modify_window);
            // 将输入控件存储为窗口的数据
            g_object_set_data(G_OBJECT(modify_window), "card_entry", card_entry);
            g_object_set_data(G_OBJECT(modify_window), "name_entry", name_entry);
			 g_object_set_data(G_OBJECT(modify_window), "gender_combo", gender_combo);
            g_object_set_data(G_OBJECT(modify_window), "birthday_calendar", birthday_calendar);
            g_object_set_data(G_OBJECT(modify_window), "phone_entry", phone_entry);
            g_object_set_data(G_OBJECT(modify_window), "type_combo", type_combo);
            g_object_set_data(G_OBJECT(modify_window), "times_entry", times_entry);
            g_object_set_data(G_OBJECT(modify_window), "expiry_calendar", expiry_calendar);
            g_object_set_data(G_OBJECT(modify_window), "parent_window", window);
            g_object_set_data(G_OBJECT(modify_window), "parent_treeview", treeview);
            gtk_box_pack_start(GTK_BOX(btn_hbox), save_btn, TRUE, TRUE, 5);

            // 取消按钮
            GtkWidget *cancel_btn = gtk_button_new_with_label("取消");
            g_signal_connect(cancel_btn, "clicked", G_CALLBACK(gtk_widget_destroy), modify_window);
            gtk_box_pack_start(GTK_BOX(btn_hbox), cancel_btn, TRUE, TRUE, 5);

            free(member);
            gtk_widget_show_all(modify_window);
        }
        
        g_free(card_id);
    } else {
        GtkWidget *dialog = gtk_message_dialog_new(
            GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK, "请先选择要修改的会员"
        );
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }
}

// 创建记录查询窗口
void create_record_window() {
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "出入场记录查询");
    gtk_window_set_default_size(GTK_WINDOW(window), 1000, 600);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);

    // 查询条件（卡号）
    GtkWidget *query_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), query_hbox, FALSE, FALSE, 5);

    GtkWidget *card_label = gtk_label_new("卡号（空表示查询所有）:");
    GtkWidget *card_entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(card_entry), 10);
    GtkWidget *query_btn = gtk_button_new_with_label("查询");
    g_signal_connect(query_btn, "clicked", G_CALLBACK(record_query_clicked), window);
    g_object_set_data(G_OBJECT(window), "card_entry", card_entry);

    gtk_box_pack_start(GTK_BOX(query_hbox), card_label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), card_entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(query_hbox), query_btn, FALSE, FALSE, 5);

    // 记录列表
    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 5);

    // 列表定义（卡号、姓名、入场时间、离场时间）
    GtkListStore *store = gtk_list_store_new(4,
        G_TYPE_STRING,  // 卡号
        G_TYPE_STRING,  // 姓名
        G_TYPE_STRING,  // 入场时间
        G_TYPE_STRING   // 离场时间
    );
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    g_object_unref(store);
    g_object_set_data(G_OBJECT(window), "treeview", treeview);

    // 添加列
    GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
    GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("卡号", renderer, "text", 0, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    column = gtk_tree_view_column_new_with_attributes("会员姓名", renderer, "text", 1, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    column = gtk_tree_view_column_new_with_attributes("入场时间", renderer, "text", 2, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    column = gtk_tree_view_column_new_with_attributes("离场时间", renderer, "text", 3, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

    gtk_container_add(GTK_CONTAINER(scrolled_window), treeview);

    // 初始加载所有记录
    refresh_record_list(treeview, "");

    gtk_widget_show_all(window);
}

// 记录查询按钮回调
static void record_query_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *window = GTK_WIDGET(data);
    GtkWidget *card_entry = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "card_entry"));
    GtkWidget *treeview = GTK_WIDGET(g_object_get_data(G_OBJECT(window), "treeview"));
    const char *card_id = gtk_entry_get_text(GTK_ENTRY(card_entry));
    refresh_record_list(treeview, card_id);
}
void refresh_record_list(GtkWidget *treeview, const char *card_id) {
    GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)));
    gtk_list_store_clear(store);

    int count;
    Record *records = (strlen(card_id) > 0) ? 
        get_records_by_card_id(card_id, &count) : 
        get_all_records(&count);

    if (records && count > 0) {
        // 先获取所有会员信息以提高效率
        int member_count;
        Member *all_members = get_all_members(&member_count);
        
        for (int i = 0; i < count; i++) {
            // 查找会员姓名
            char name[21] = "未知会员";
            if (all_members) {
                for (int j = 0; j < member_count; j++) {
                    if (strcmp(all_members[j].card_id, records[i].card_id) == 0) {
                        strncpy(name, all_members[j].name, sizeof(name)-1);
                        name[sizeof(name)-1] = '\0';
                        break;
                    }
                }
            }

            GtkTreeIter iter;
            gtk_list_store_append(store, &iter);
            gtk_list_store_set(store, &iter,
                0, records[i].card_id,
                1, name,
                2, records[i].in_time,
                3, (strlen(records[i].out_time) > 0) ? records[i].out_time : "未离场",
                -1);
        }
        
        if (all_members) free(all_members);
        free(records);
    }
}
